import AjaxTool from './ajaxTool.js'
import Canvas2Image from './canvas2image.js'
const {ajax} = AjaxTool

let token = localStorage.getItem('token')
if (!token) {
    location.href = '../index.html'
}
const baseUrl = 'http://shopback.bluej.cn/api/'
let userInfo = JSON.parse(localStorage.getItem('userinfo'))
let bookData = JSON.parse(localStorage.getItem('bookData'))
let qrNeed = {
    token,
    id: bookData.id
}

let username = document.querySelector('#name')
let mesText = document.querySelector('#mes-text')
let qrImg = document.querySelector('#qrImg')
let saveBtn = document.querySelector('#save-btn')

let bookTempDateArr = bookData.day_time.split('-')
let bookTempTimeArr = bookData.show_name.split('-')

let mesStr = ''
let imgurl = ''
username.innerHTML = `
    <div class="name-text">${userInfo.name}</div>
    <span>:</span>
`
mesStr = `
欢迎您在${bookTempDateArr[1]}月${bookTempDateArr[2]}日${bookTempTimeArr[0]}参观${bookTempDateArr[0]}广州国际花卉艺术展暨中国插花花艺展。
`
mesText.innerText = mesStr

ajax('GET', `${baseUrl}f_user/Qrcode`, qrNeed).then(res => {
    // console.log(JSON.parse(res));
    let result = JSON.parse(res)
    const { msg } = result
    if (msg == '操作成功') {
        const { data } = result
        imgurl = data.data
        qrImg.src = data.data
        // saveBtn.download = 'save.png'
    }
})

saveBtn.addEventListener('click', function (e) {
    e.preventDefault()
    new html2canvas(document.documentElement, {
        allowTaint: false,
        useCORS: true,
    }).then(canvas => {
        // Canvas2Image.saveAsJPEG(canvas)
        let img  = Canvas2Image.convertToJPEG(canvas,window.innerWidth,window.innerHeight)
        img.style.position = 'absolute'
        img.style.top = 0
        let tip =  document.createElement('div')
        tip.classList.add('img-tip')
        tip.innerText = '请长按保存截图'
        document.body.appendChild(img)
        document.body.appendChild(tip)
    });
})